﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GDCal.vo;
using GDCal.dao;
using System.Collections;

namespace GDCal
{
    public partial class StreetTypeDetailForm : Form
    {
        private StreetTypeVO streetTypeVO;

        public StreetTypeDetailForm(StreetTypeVO streetTypeVO)
        {
            Int32 currentId = 0;
            Int32 parentId = 0;
            InitializeComponent();
            if (streetTypeVO != null)
            {
                this.streetTypeVO = streetTypeVO;
                this.streetTypeValue.Text = this.streetTypeVO.getType();
                this.streetTypeDescValue.Text = this.streetTypeVO.getDescription();
                currentId = this.streetTypeVO.getId();
                parentId = this.streetTypeVO.getParentId();
                this.isDefault.Checked = this.streetTypeVO.getIsDefault() == 1;
                this.streetGroupVal.Value = this.streetTypeVO.getStreetGroup();

                Int32 foundId = StreetTypeDao.getStreetIdOfStreetGroupHasDefault(Convert.ToInt32(this.streetGroupVal.Value));
                if (foundId > 0)
                {
                    if (this.streetTypeVO.getId() <= 0)
                        this.isDefault.Checked = false;
                    if (this.streetTypeVO.getId() != foundId)
                        this.isDefault.Enabled = false;
                }
                else
                {
                    this.isDefault.Enabled = true;
                }
            }
            ArrayList datas = StreetTypeDao.filter(new StreetTypeVO());
            int i = -1;
            foreach (StreetTypeVO streetType in datas)
            {
                i++;
                if (streetType.getId() != currentId)
                    this.parentStreetTypeCombo.Items.Add(new StreetTypeComboItem(streetType.getType(), streetType.getId()));
                if (streetType.getId() == parentId)
                    this.parentStreetTypeCombo.SelectedIndex = i;
            }
        }

        public StreetTypeVO getStreetTypeVO()
        {
            return this.streetTypeVO;
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(this.streetTypeValue.Text))
            {
                StreetTypeVO temp = StreetTypeDao.getStreetTypeByType(this.streetTypeValue.Text);
                if (temp != null && temp.getId() > 0 && temp.getId() != this.streetTypeVO.getId())
                {
                    MessageBox.Show(GDCalMessages.Notify_StreetTypeDuplicated, GDCalMessages.Notify_Caption);
                }
                else
                {
                    this.streetTypeVO.setType(this.streetTypeValue.Text);
                    this.streetTypeVO.setDescription(this.streetTypeDescValue.Text);
                    if (this.parentStreetTypeCombo.SelectedIndex >= 0)
                        this.streetTypeVO.setParentId((this.parentStreetTypeCombo.SelectedItem as StreetTypeComboItem).getValue());
                    else
                        this.streetTypeVO.setParentId(0);
                    this.streetTypeVO.setStreetGroup(Convert.ToInt32(this.streetGroupVal.Value));

                    if (this.isDefault.Checked)
                        this.streetTypeVO.setIsDefault(1);
                    else
                        this.streetTypeVO.setIsDefault(0);

                    if (this.streetTypeVO.getId() > 0)
                    {
                        StreetTypeDao.updateStreetType(streetTypeVO);
                    }
                    else
                    {
                        StreetTypeDao.insertStreetType(this.streetTypeVO);
                        this.streetTypeVO = StreetTypeDao.getStreetTypeByType(this.streetTypeVO.getType());
                    }
                    this.Close();
                }
            }
            else
            { 
                MessageBox.Show(GDCalMessages.Notify_StreetTypeEmpty, GDCalMessages.Notify_Caption);
            }
        }

        private void streetGroupVal_ValueChanged(object sender, EventArgs e)
        {
            Int32 currentId = StreetTypeDao.getStreetIdOfStreetGroupHasDefault(Convert.ToInt32(this.streetGroupVal.Value));
            if (currentId > 0)
            {
                if (this.streetTypeVO.getId() <= 0)
                    this.isDefault.Checked = false;
                if (this.streetTypeVO.getId() != currentId)
                    this.isDefault.Enabled = false;
            } else 
            {
                this.isDefault.Enabled = true;
            }
        }
    }
}
